package netty_chat;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

import java.util.Scanner;

/**
 * 聊天室客户端
 */
public class NettyChatClient {
    private String ip; // 服务器IP
    private int port; // 服务器端口号

    public NettyChatClient(String ip, int port) {
        this.ip = ip;
        this.port = port;
    }

    public void run() throws InterruptedException {
        EventLoopGroup group = null;
        try {
            // 1.创建线程组
            group = new NioEventLoopGroup();
            // 2.创建客户端启动助手
            Bootstrap bootstrap = new Bootstrap();
            // 3.设置线程组
            bootstrap.group(group)
                    // 4.设置服务端通道实现为NIO
                    .channel(NioSocketChannel.class)
                    // 5.创建一个通道初始化对象
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel sc) throws Exception {
                            //添加编解码器
                            sc.pipeline().addLast(new StringDecoder());
                            sc.pipeline().addLast(new StringEncoder());
                            // 6.向pipeline中添加自定义业务处理handler
                            sc.pipeline().addLast(new NettyChatClientHandler());
                        }
                    });
            // 7.启动客户端，等待连接服务端，同时将异步改为同步
            ChannelFuture future = bootstrap.connect(ip, port).sync();
            Channel channel = future.channel();
            // 显示本机地址
            System.out.println("-------" +
                    channel.localAddress().toString().substring(1) + "--------");
            Scanner scanner = new Scanner(System.in);
            while (scanner.hasNextLine()) {
                String msg = scanner.nextLine();
                // 向服务端发送消息
                channel.writeAndFlush(msg);
            }
            // 8.关闭通道和关闭连接池
            future.channel().closeFuture().sync();
        } finally {
            group.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        new NettyChatClient("127.0.0.1", 9998).run();
    }
}
